package org.example.controller;

import org.example.Contant.Result;
import org.example.common.UserContextHolder;
import org.example.entity.User;
import org.example.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @ResponseBody
    @PostMapping("/login")
    public Result doLogin(@RequestParam String username, @RequestParam String password) {
        String token = loginService.login(username, password);
        return Result.success("200",token);
    }

    // 退出登录
    @GetMapping("/logout")
    public String logout() {
        return "redirect:/api/login";
    }
}
